/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package address;


import equipment.Equipment;
import exemple.ProcessusB;
import exemple.HistoricSystem;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import exemple.ClientRMI;

import exemple.HistoricSystemDAO;
import exemple.ProcessusBDAO;
import java.net.URI;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.apache.struts2.ServletActionContext;



/**
 *
 * @author PC
 */
public class AddressIpAction extends ActionSupport implements ModelDriven<Addresses> {

    private static final long serialVersionUID = 9149826260758390091L;
    //private int id_equipment;
    private int id_address;
    private String name_hardware;
    private String cpu;
    private String ram;
    private String lieu;
    private String system_ex;
    private String address;
    private String netmask;
    private String interface_addr;
    private String type_address;
    private List<AddressIp> listAddressIp;
    private List<ProcessusB> listProcess;
    private List<HistoricSystem> listHistoric;
    private AddressIpDAO addressDao;
    private AddressIp addressIp;
    private HistoricSystem historicSystem;
    private HistoricSystemDAO historicSystemDao;
    private ProcessusBDAO processusDao;
    private Addresses addresses;
    //private Equipment equipment;

    @Override
    public String execute() {
        System.out.println("-----method execute");
        

        if (checkForm()) {
            AddressIp addressip = new AddressIp(id_address, name_hardware, address, netmask, type_address);
            addressDao = new AddressIpDAO();
            addressDao.add(addressip);
            addressDao.listAddress();
            return SUCCESS;
        } else {
            System.out.println("-----erreur form");
            addressIp = new AddressIp();
            return ERROR;
        }
    }
    
    public String displayform() {
        addressIp = new AddressIp();
        return SUCCESS;
    }

    public String getListAddress() {
        addressDao = new AddressIpDAO();
        listAddressIp = addressDao.listAddress();
        return SUCCESS;
    }
    
    /**
     * Whether the form is properly completed
     * @return true or false
     */
    private boolean checkForm() {
        System.out.println("-----method checkForm");
        boolean is_correct = checkAddressOrNetmask();      
        if(name_hardware.isEmpty()) {
            addFieldError("name_hardware", getText("error.name_hardware"));
            is_correct = false;
        }       
        System.out.println("is_correct: " + is_correct);
        return is_correct;
    }
    /**
     * Checks if the input address is correct and the netmask if it is not empty
     * @return true or false 
     */
    private boolean checkAddressOrNetmask() {
        System.out.println("-----method checkAddressOrNetmask"); 
        boolean is_correct = true;
        final String IP_ADDRESS_PATTERN_IPV4 = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";
        if(!address.isEmpty()) {
            Matcher matcher = Pattern.compile(IP_ADDRESS_PATTERN_IPV4).matcher(address);
            if (matcher.find()) {
            // OK
            }
            else  {
                addFieldError("address", getText("error.address"));
                is_correct = false;
            }
        }
        else {
            addFieldError("address", getText("error.addressEmpty"));
            is_correct = false;
        }          


        if(!netmask.isEmpty()) {
            Matcher matcher = Pattern.compile(IP_ADDRESS_PATTERN_IPV4).matcher(netmask);
            if (matcher.find()) {
            // OK
            }
            else  {
                addFieldError("netmask", getText("error.netmask"));
                is_correct = false;
            }
        }
            
        return is_correct;
    }
    

    public int getId_address() {
        return id_address;
    }

    public void setId_address(int id_address) {
        this.id_address = id_address;
    }
      
    public String getName_hardware() {
        return name_hardware;
    }

    public void setName_hardware(String name_hardware) {
        this.name_hardware = name_hardware;
    }

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getRam() {
        return ram;
    }

    public void setRam(String ram) {
        this.ram = ram;
    }

    public String getLieu() {
        return lieu;
    }

    public void setLieu(String lieu) {
        this.lieu = lieu;
    }

    public String getSystem_ex() {
        return system_ex;
    }

    public void setSystem_ex(String system_ex) {
        this.system_ex = system_ex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    
    public String getNetmask() {
        return netmask;
    }

    public void setNetmask(String netmask) {
        this.netmask = netmask;
    }
    public String getType_address() {
        return type_address;
    }

    public void setType_address(String type_address) {
        this.type_address = type_address;
    }

    public List<AddressIp> getListAddressIp() {
        return listAddressIp;
    }

    public void setListAddressIp(List<AddressIp> listAddressIp) {
        this.listAddressIp = listAddressIp;
    }  

    public List<ProcessusB> getListProcess() {
        return listProcess;
    }

    public void setListProcess(List<ProcessusB> listProcess) {
        this.listProcess = listProcess;
    }

    public HistoricSystem getHistoricSystem() {
        return historicSystem;
    }

    public void setHistoricSystem(HistoricSystem historicSystem) {
        this.historicSystem = historicSystem;
    }

    public List<HistoricSystem> getListHistoric() {
        return listHistoric;
    }

    public void setListHistoric(List<HistoricSystem> listHistoric) {
        this.listHistoric = listHistoric;
    }
    
    
    
    public String editAddressIp() {
        AddressIpDAO addressIpDao = new AddressIpDAO();
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
        System.out.println("id: "+request.getParameter("id_address"));
        addressIp = addressIpDao.getAddressById(Integer.parseInt(request.getParameter("id_address")));
        return SUCCESS;
    }
    
    public String deleteAddressIp() {
        AddressIpDAO addressIpDao = new AddressIpDAO();
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
        addressIpDao.deleteAddress(Integer.parseInt(request.getParameter("id_address")));
        listAddressIp = addressIpDao.listAddress();
        return SUCCESS;
    }
    
    public String detailAddressIp() {
        AddressIpDAO addressIpDao = new AddressIpDAO();
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        System.out.println("id: " + request.getParameter("id_address"));
        addressIp = addressIpDao.getAddressById(Integer.parseInt(request.getParameter("id_address")));
        
        
        listProcess = ClientRMI.getListOfProcessus(addressIp.getAddress());
        //Facon RMI direct
        //historicSystem = Client.getSystemInformations(addressIp.getAddress());

        //Passage par un web service rest
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        
        WebResource service = client.resource(getBaseURI(addressIp.getAddress()));
        historicSystem = service.path("systemInformations").path("getSystemInfo").path(addressIp.getAddress()).accept(MediaType.APPLICATION_XML).get(HistoricSystem.class);
        historicSystem.setAddress(addressIp);
        historicSystemDao = new HistoricSystemDAO();
        historicSystemDao.add(historicSystem);
        
        processusDao = new ProcessusBDAO();
        processusDao.add(listProcess,historicSystem);

        System.out.println("test : "+historicSystem);
        
        return SUCCESS;
    }
    
    public String checkHisto() {
        AddressIpDAO addressIpDao = new AddressIpDAO();
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        System.out.println("id: " + request.getParameter("id_address"));
        addressIp = addressIpDao.getAddressById(Integer.parseInt(request.getParameter("id_address")));
       
        historicSystemDao = new HistoricSystemDAO();
        listHistoric = historicSystemDao.listHistroic(addressIp.getId_address());
        
        return SUCCESS;
    }
    
    public String detailHisto() {
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        int id_historic = Integer.parseInt(request.getParameter("id_historic"));

        historicSystemDao = new HistoricSystemDAO();
        historicSystem = historicSystemDao.getHisto(id_historic);
        
        processusDao = new ProcessusBDAO();
        listProcess = processusDao.listProcess(id_historic);
        
        return SUCCESS;
    }
    
    public String supp_histo() {
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        
        AddressIpDAO addressIpDao = new AddressIpDAO();
        addressIp = addressIpDao.getAddressById(Integer.parseInt(request.getParameter("id_address")));

        historicSystemDao = new HistoricSystemDAO();
        historicSystemDao.supp_histo(addressIp.getId_address());
 
        
        return SUCCESS;
    }
    
     //en cours de developpement 

    public AddressIp getAddressIp() {
        return addressIp;
    }

    public void setAddressIp(AddressIp addressIp) {
        this.addressIp = addressIp;
    }

    @Override
    public Addresses getModel() {
        return addresses;
    }

    private static URI getBaseURI(String ip) {
        //return UriBuilder.fromUri("http://"+ip+":8080/WebApplication/webresources").build();
        return UriBuilder.fromUri("http://localhost:8080/WebServiceAdminSystem/webresources").build();
    }

    
}
